#include "EnemyManager.h"
#include "EntityEnemy.h"
#include "WorldModel.h"

CEnemyManager* CEnemyManager::instance=0;

CEnemyManager::CEnemyManager(void): indexFirstEnemy(-1), indexLastEnemy(-1)
{}

CEnemyManager::~CEnemyManager(void)
{
	for(vector<CEntityEnemy*>::iterator p=this->vectorEnemyTemplate.begin();p<this->vectorEnemyTemplate.end();p++)
	{
		delete (*p);
		(*p)=0;
	}
}

CEnemyManager& CEnemyManager::getInstance(void)
{
	if(instance==0)
	{
		instance = new CEnemyManager();
	}
	return *instance;
}

/*function that delete and close the enemy manager*/
void CEnemyManager::Close(void)
{
	delete instance;
	instance=0;
}

/*create a template of enemy that can be use after to spawn an enemy*/
void CEnemyManager::createEnemyTemplate(int indexSprite, int indexFrame, int health, int collisionCost, 
										  int projectileType)
{
	CEntityEnemy* newEnemy=new CEntityEnemy(0, 0, indexSprite, indexFrame, 0,health, 
		collisionCost,0,0, projectileType, true);
	this->vectorEnemyTemplate.push_back(newEnemy);
}

/*spawn an enemy in the world*/
void CEnemyManager::spawnEnemy(int indexEnemyTemplate, int timeToSpawn, int timeToLive, CRoute* const route,
								 int speed, int timeBetweenProjectile)
{
	if(this->indexFirstEnemy==-1)
	{
		this->indexFirstEnemy=WMODEL.getEntityVectorSize();
		this->indexLastEnemy=this->indexFirstEnemy-1;
	}
	this->indexLastEnemy++;
	CEntityEnemy* newEnemy=new CEntityEnemy();
	(*newEnemy)=(*this->vectorEnemyTemplate[indexEnemyTemplate]);
	newEnemy->setTimeToSpawn(timeToSpawn);
	newEnemy->setTimeToLive(timeToLive);
	newEnemy->setRoute(route);
	newEnemy->setTimeBetweenProjectile(timeBetweenProjectile);
	newEnemy->setSpeed(speed);
	WMODEL.addEntity(newEnemy);
}
int CEnemyManager::getIndexFirstEnemy(void)const
{
	return this->indexFirstEnemy;
}
int CEnemyManager::getIndexLastEnemy(void)const
{
	return this->indexLastEnemy;
}
